package com.iamk.util;

import ij.process.ColorProcessor;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import com.iamk.lib.FeaturesColor;

import de.lmu.ifi.dbs.jfeaturelib.features.MPEG7ColorLayout;
import de.lmu.ifi.dbs.jfeaturelib.features.ReferenceColorSimilarity;
import de.lmu.ifi.dbs.utilities.Arrays2;


public class CreateDataWavelet {
	public static void main(String[] args) {
		NumberFormat nf = new DecimalFormat("#.######");
		BufferedWriter bw = null;
		ListFilesUtil lfu = new ListFilesUtil();
		lfu.listAllFiles("E:\\dataset\\MSRC_binhvt\\ImageTrain");
		HaarTransform mHaar = new HaarTransform();
		CreateImageRegion cir;
		String[] labels = { "1", "2", "3", "6", "9", "17", "20" };
		try {
			bw = new BufferedWriter(new FileWriter("features-wavelet-mycolor-train.arff",true));
			// Writer header file arff
			bw.write("@RELATION feature-tamura\n");
			for (int i = 0; i < 18; i++) {
				bw.write("@ATTRIBUTE f" + (i + 1) + "	REAL\n");
			}
			bw.write("@attribute class {1,2,3,6,9,17,20}\n");
			bw.write("@data\n");
			
			for(int f = 0; f < lfu.arrAllFile.size();f++){
				System.out.println(lfu.arrFileName.get(f));
				String filename = lfu.arrFileName.get(f).replace(".jpg", "");
				filename = filename.replace(".bmp", "");
				cir = new CreateImageRegion(lfu.arrAllFile.get(f), "E:\\dataset\\MSRC_binhvt\\MaskTrain\\"+filename+".txt");
				cir.loadData(cir.imgPath, cir.maskPath);
				
				for(int i = 0; i < cir.listRegion.size(); i++){
					if(getIndex(labels, cir.listLabel.get(i))!=-1){
					BufferedImage imgWavelet = mHaar.foward(ImageUtil.imageToBufferedImage(cir.listRegion.get(i)), 1);
					BufferedImage imgApp = imgWavelet.getSubimage(0, 0, imgWavelet.getWidth()/2, imgWavelet.getHeight()/2);
					BufferedImage imgD = imgWavelet.getSubimage(imgWavelet.getWidth()/2, imgWavelet.getHeight()/2, imgWavelet.getWidth()/2, imgWavelet.getHeight()/2);
					
//					MPEG7ColorLayout mDescriptorApp = new MPEG7ColorLayout();
//			        mDescriptorApp.run(new ColorProcessor(imgApp));
//			        List<double[]> featuresApp = mDescriptorApp.getFeatures();
//					for (double[] feature : featuresApp) {
//						bw.write(Arrays2.join(feature, ", ", "%.5f"));
//					}
//					
//					MPEG7ColorLayout mDescriptorD = new MPEG7ColorLayout();
//			        mDescriptorD.run(new ColorProcessor(imgD));
//			        List<double[]> featuresD = mDescriptorD.getFeatures();
//			        bw.write(", ");
//					for (double[] feature : featuresD) {
//						bw.write(Arrays2.join(feature, ", ", "%.5f"));
//					}
//					bw.write(", " + cir.listLabel.get(i) + "\n");
					
					FeaturesColor mColorDescriptorApp = new FeaturesColor(imgApp);
			        List<Double> featuresApp = mColorDescriptorApp.getFeatures();
					for (int j = 0; j < featuresApp.size(); j++) {
						bw.write(nf.format(featuresApp.get(j)) + ", ");
					}
					FeaturesColor mColorDescriptorD = new FeaturesColor(imgD);
			        List<Double> featuresD = mColorDescriptorD.getFeatures();
					for (int j = 0; j < featuresD.size(); j++) {
						bw.write(nf.format(featuresD.get(j)) + ", ");
					}
					bw.write(cir.listLabel.get(i) + "\n");
				}
			}}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static int getIndex(String[] arr, float value) {
		int i = -1;
		for (int j = 0; j < arr.length; j++) {
			if (value==Float.parseFloat(arr[j])){ i=j;
			break;}
		}
		System.out.println(i);
		return i;
	}
}
